package list;

public class List2_zhongjianjiedian {

    public static void main(String[] args) {
        Node head = new Node(7);
        head.next = new Node(3);
        head.next.next= new Node(8);
        head.next.next.next= new Node(5);
        head.next.next.next.next= new Node(6);
        head.next.next.next.next.next= new Node(9);
        head.next.next.next.next.next.next= new Node(18);

        Node mid = middleNode(head);
        System.out.println(mid.value);
    }

    private static Node middleNode(Node head) {
        Node fast = head;//快引用
        Node slow = head;//慢引用
        while ( fast !=null && fast.next!=null ){//fast在范围内并且它的下一个也不是null
            fast = fast.next.next;//fast向后引用两个节点
            slow = slow.next;//slow向后引用一个节点
        }
        return slow;//slow所引用的节点就是中间节点
    }


    static class Node{
        private int value;
        private Node prev;
        private Node next;
        public Node(int value){
            this.value=value;
        }
    }
}
